Page({
    data: {
        latitude: '31.25392392549918',
        longitude: '120.75753402740479',
        scale: 15,
        sameCityHosLatLng: [],
        array: [],
        markers: [],
        data_latitude: '',
        data_longitude: '',
        showProject: false,
        columnsProject: [{
            text: '查看医院信息',
            value: 1
        }, {
            text: '查看医院具体位置',
            value: 2
        }]
    },
    onLoad(options) {
        var that = this
        that.setData({
            sameCityHosLatLng: JSON.parse(options.sameCityHosLatLng),
            hospitalId:options.hospitalId
        })
        console.log(that.data.sameCityHosLatLng, '这是地图页面')
        that.data.sameCityHosLatLng.forEach((item, index) => {
            that.data.markers.push({
                    id: Number(item.lbmId),
                    latitude: item ? item.lat : '',
                    longitude: item ? item.lng : '',
                    name: item.lbmName,
                    label: {
                        content: item.lbmName,
                        color: '#000000',
                        bgColor: "#fff000",
                        borderRadius: 10,
                        padding: 5
                    },
                    callout: {
                        content: item.lbmName,
                        color: "#000"
                    }
                }),
                that.setData({
                    markers: that.data.markers
                })
            console.log(that.data.markers, '我是markers')
        })
    },
    onReady: function (e) {
        var that = this
        that.mapCtx = wx.createMapContext('myMap')
        wx.getLocation({
            type: "wgs84",
            success: function (res) {
                that.setData({
                    latitude: res.latitude,
                    longitude: res.longitude
                })
            }
        })
    },
    getCenterLocation: function () {
        var that = this
        that.mapCtx.getCenterLocation({
            success: function (res) {
                that.setData({
                    longitude: res.longitude,
                    latitude: res.latitude
                })
            }
        })
    },
    moveToLocation: function () {
        this.mapCtx.moveToLocation()
    },
    translateMarker: function () {
        this.mapCtx.translateMarker({
            markerId: 1,
            autoRotate: true,
            duration: 1000,
            destination: {
                latitude: 23.10229,
                longitude: 113.3345211,
            },
            animationEnd() {
                console.log('animation end')
            }
        })
    },
    includePoints: function () {
        this.mapCtx.includePoints({
            padding: [10],
            points: [{
                latitude: 23.10229,
                longitude: 113.3345211,
            }, {
                latitude: 23.00229,
                longitude: 113.3345211,
            }]
        })
    },
    changeHandel(e) {
        console.log(e)
        // this.data.array.forEach(()=>{
        //   this.data.markers.pop()
        // })
        // this.setData({
        //   data_latitude: e.detail.latitude,
        //   data_longitude: e.detail.longitude,
        //   array:[],
        //   markers:this.data.markers
        // })
        // this.data.array.push(
        //   {
        //     id: e.detail.latitude,
        //     latitude: e.detail.latitude,
        //     longitude: e.detail.longitude,
        //     name: '新标记点',
        //     label: {
        //       content: "东方文慧园",
        //       color: '#000000',
        //       bgColor: "#fff000",
        //       borderRadius: 10,
        //       padding: 20

        //     },
        //     callout: {
        //       content: "东方文慧园",
        //       color: "#000"
        //     }
        //   }
        // )
        // this.setData({
        //   markers:this.data.markers.concat(this.data.array)
        // })
    },
    gotohere(res) {
        var that = this
        console.log(res);
        let lat = ''; // 获取点击的markers经纬度
        let lon = ''; // 获取点击的markers经纬度
        let name = ''; // 获取点击的markers名称
        let markerId = res.markerId; // 获取点击的markers  id
        let markers = that.data.markers; // 获取markers列表

        for (let item of markers) {
            if (item.id === markerId) {
                if(that.data.hospitalId==markerId){
                    console.log(55,item)
                    wx.openLocation({ // 打开微信内置地图，实现导航功能（在内置地图里面打开地图软件）
                        latitude: item.latitude,
                        longitude: item.longitude,
                        name: item.name,
                        success: function (res) {
                            console.log(res, '111');
                        },
                        fail: function (res) {
                            console.log(res, '222');
                        }
                    })
                }else{
                 this.setData({
                    showProject: true,
                    tlat: item.latitude,
                    tlon: item.longitude,
                    tname: item.callout.content,
                    placeId: item.id
                })   
                }
                
                break;
            }
        }

    },
    changeThis(e) {
        // console.log(e)
        this.setData({
            // latitude: e.detail.centerLocation.latitude,
            // longitude: e.detail.centerLocation.longitude,
        })
    },
    // 默认显示在目标点
    changeScaleAA() {
        var that = this
        // that.mapCtx = wx.createMapContext('myMap')
        wx.getLocation({
            type: "wgs84",
            success: function (res) {
                that.setData({
                    scale: 14,
                    latitude: that.data.sameCityHosLatLng[0].lat,
                    longitude: that.data.sameCityHosLatLng[0].lng,
                })
            }
        })
    },
    changeScale() {
        var that = this
        // that.mapCtx = wx.createMapContext('myMap')
        wx.getLocation({
            type: "wgs84",
            success: function (res) {
                that.setData({
                    scale: 14,
                    latitude: res.latitude,
                    longitude: res.longitude
                })
            }
        })
    },

    //导航
    onGuideTap: function (event) {
        var lat = Number(event.currentTarget.dataset.latitude);
        var lon = Number(event.currentTarget.dataset.longitude);
        var bankName = event.currentTarget.dataset.bankname;
        console.log(lat);
        console.log(lon);
        wx.openLocation({
            type: 'gcj02',
            latitude: lat,
            longitude: lon,
            name: bankName,
            scale: 28
        })
    },
    //关闭弹出层
    onCloseProject() {
        this.setData({
            showProject: false
        })
    },
    cancelject() {
        this.setData({
            showProject: false
        })
    },
    okject(e) {
        let that = this
        if (e.detail.value.value == 1) {
            wx.navigateTo({
                url: `/pages/hospitalDetail/hospitalDetail?hospitalId=${that.data.placeId}`
            })
        } else {
            wx.openLocation({ // 打开微信内置地图，实现导航功能（在内置地图里面打开地图软件）
                latitude: that.data.tlat,
                longitude: that.data.tlon,
                name: that.data.tname,
                success: function (res) {
                    console.log(res, '111');
                },
                fail: function (res) {
                    console.log(res, '222');
                }
            })
        }
        that.setData({
            showProject: false
        })
    }
})